﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace TheCursedNavy.Eve.Google.Calendar
{
	public partial class Form1 : Form
	{
		private readonly Regex skillEntry =
			new Regex(@"^(?<skill>.+) \(Start: (?<start>.+); Finish: (?<end>.+)\)$", RegexOptions.Compiled);

		public Form1()
		{
			InitializeComponent();

			_calendarService = new CalendarService("aensidhe-evestuff-0.1");
			_calendarService.setUserCredentials(_email.Text, _password.Text);
		}

		private readonly CalendarService _calendarService;
		private void button1_Click(object sender, EventArgs e)
		{
			if (!ValidateInput())
				return;
					
			CalendarEntry calendar = GetCalendar(_calendarName.Text);
			calendar.Selected = true;

			_calendarService.Update(calendar);

			EventQuery eventQuery = new EventQuery(string.Format("http://www.google.com/calendar/feeds/{0}/private/full", _email.Text));
			//EventQuery eventQuery = new EventQuery(calendar.SelfUri.ToString());
			eventQuery.StartTime = eventQuery.EndTime = DateTime.Now;
			EventFeed events = _calendarService.Query(eventQuery);
			foreach (string planString in textBox1.Lines)
			{
				EventEntry skill = (EventEntry) events.CreateFeedEntry();
				PlanEntry planEntry = ParseString(planString);
				skill.Times.Add(new When(planEntry.Start, planEntry.End));
				skill.Title = new AtomTextConstruct(AtomTextConstructElementType.Title, planEntry.Skill);

				_calendarService.Insert(events, skill);
			}
		}

		private CalendarEntry GetCalendar(string name)
		{
			CalendarQuery calendarQuery = new CalendarQuery("http://www.google.com/calendar/feeds/default/owncalendars/full");

			CalendarFeed feed = _calendarService.Query(calendarQuery);

			foreach (CalendarEntry calendarEntry in feed.Entries)
				if (calendarEntry.Title.Text == name)
					return calendarEntry;

			CreateNewCalendar(feed, name);

			return GetCalendar(name);
		}

		private static void CreateNewCalendar(CalendarFeed feed, string name) 
		{
			CalendarEntry newCalendar = (CalendarEntry) feed.CreateFeedEntry();

			newCalendar.Title = new AtomTextConstruct(AtomTextConstructElementType.Title, name);

			feed.Insert(newCalendar);
		}

		private PlanEntry ParseString(string planString)
		{
			Match m = skillEntry.Match(planString);

			PlanEntry result = new PlanEntry();
			result.Skill = m.Groups["skill"].Value;
			result.Start = DateTime.Parse(m.Groups["start"].Value);
			result.End = DateTime.Parse(m.Groups["end"].Value);

			return result;
		}

		private bool ValidateInput()
		{
			_errorProvider.Clear();
			bool errorsArePresent = false;

			if (string.IsNullOrEmpty(_calendarName.Text))
			{
				_errorProvider.SetError(_calendarName, "Calendar name can't be empty");
				errorsArePresent = true;
			}

			if (string.IsNullOrEmpty(_email.Text))
			{
				_errorProvider.SetError(_email, "E-mail can't be empty");
				errorsArePresent = true;
			}

			if (string.IsNullOrEmpty(_password.Text))
			{
				_errorProvider.SetError(_password, "Password can't be empty");
				errorsArePresent = true;
			}

			return !errorsArePresent;
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}
	}

	internal struct PlanEntry
	{
		public DateTime End;
		public string Skill;
		public DateTime Start;
	}
}